package com.beta.admin.controller;
import com.baomidou.mybatisplus.plugins.Page;
import com.beta.admin.request.admin.CourierRequest;
import com.beta.admin.vo.CourierVo;
import com.beta.common.web.AbstractBaseController;
import com.beta.common.web.ContentType;
import com.beta.dto.admin.CourierDto;
import com.beta.entity.Couriers;
import com.beta.service.ICouriersService;
import com.beta.service.IShipmentsService;
import ma.glasnost.orika.MapperFacade;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;

/**
 * Created by lichao on 2016/12/30.
 */
@RestController
@RequestMapping(value =RequestMappingConstants.MAPPING_PREFIX + "/couriers")
public class CouriersController extends AbstractBaseController {
    @Autowired
    private ICouriersService couriersService;
    @Autowired
    private MapperFacade baseMapper;
    @Autowired
    private IShipmentsService shipmentsService;
    @RequiresPermissions("couriers:list")
    @GetMapping(value = "",produces = {ContentType.JSON})
    public Object getCouriers(@RequestParam("text")String text){
        Page<Couriers> couriersPage = couriersService.getCouriers(getMybatisPage(),text);
        return pageData(couriersPage);
    }

    @RequiresPermissions("couriers:view")
    @GetMapping(value = "/{courierId}",produces = {ContentType.JSON})
    public Object getCourierById(@PathVariable("courierId")Integer courierId){
        Couriers courier = couriersService.getCourierById(courierId);
        CourierVo courierVo= baseMapper.map(courier,CourierVo.class);
        return ok(courierVo);
    }

    @RequiresPermissions("couriers:new")
    @PostMapping(value = "",produces = {ContentType.JSON})
    public Object createCourier(@Valid @RequestBody CourierRequest courierRequest){
        CourierDto courierDto=new CourierDto();
        courierDto.setName(courierRequest.getName());
        courierDto.setCityCode(courierRequest.getCityCode());
        courierDto.setCode(courierRequest.getCode());
        courierDto.setContact(courierRequest.getContact());
        courierDto.setNotes(courierRequest.getNotes());
        courierDto.setPassword(courierRequest.getPassword());
        courierDto.setPhone(courierRequest.getPhone());
        courierDto.setStatus(courierRequest.getStatus());
        couriersService.createCourier(courierDto);
        return null;
    }

    @RequiresPermissions("couriers:new")
    @PutMapping(value = "/{courierId}",produces = ContentType.JSON)
    public Object updateCourier(@PathVariable("courierId") Integer courierId,@Valid@RequestBody CourierRequest courierRequest){
        CourierDto courierDto=new CourierDto();
        courierDto.setName(courierRequest.getName());
        courierDto.setCityCode(courierRequest.getCityCode());
        courierDto.setCode(courierRequest.getCode());
        courierDto.setContact(courierRequest.getContact());
        courierDto.setNotes(courierRequest.getNotes());
        courierDto.setPassword(courierRequest.getPassword());
        courierDto.setPhone(courierRequest.getPhone());
        courierDto.setStatus(courierRequest.getStatus());
        couriersService.updateCourier(courierId,courierDto);
        return null;
    }
    @RequiresPermissions("couriers:edit")
    @DeleteMapping(value = "/{courierId}",produces = ContentType.JSON)
    public Object deleteCourier(@PathVariable("courierId") Integer courierId){
       couriersService.deleteCourier(courierId);
        return null;
    }

}
